package com.zenithmind.chat.controller;

import com.zenithmind.chat.pojo.dto.ChatFriendRequestDTO;
import com.zenithmind.chat.pojo.vo.ChatFriendVO;
import com.zenithmind.chat.service.ChatFriendService;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 好友管理控制器
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/chat/friend")
@RequiredArgsConstructor
@Tag(name = "好友管理", description = "好友相关接口")
public class ChatFriendController {

    private final ChatFriendService friendService;

    @GetMapping("/page")
    @Operation(summary = "分页查询好友列表", description = "支持分组和关键词搜索")
    public Result<PageResult<ChatFriendVO>> getFriendPage(
            @Parameter(description = "分组名称") @RequestParam(required = false) String groupName,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "20") Integer pageSize) {
        String userId = UserContext.getUserId();
        PageResult<ChatFriendVO> result = friendService.getFriendPage(userId, groupName, keyword, pageNum, pageSize);
        return Result.success(result);
    }

    @GetMapping("/list")
    @Operation(summary = "获取好友列表", description = "获取用户的所有好友")
    public Result<List<ChatFriendVO>> getFriendList(
            @Parameter(description = "关系状态") @RequestParam(required = false) Integer status) {
        String userId = UserContext.getUserId();
        List<ChatFriendVO> friends = friendService.getUserFriends(userId, status);
        return Result.success(friends);
    }

    @GetMapping("/{friendId}")
    @Operation(summary = "获取好友详情", description = "根据ID获取好友详细信息")
    public Result<ChatFriendVO> getFriendById(
            @Parameter(description = "好友ID") @PathVariable String friendId) {
        String userId = UserContext.getUserId();
        ChatFriendVO friend = friendService.getFriendById(userId, friendId);
        return Result.success(friend);
    }

    @PostMapping("/request")
    @Operation(summary = "发送好友申请", description = "向指定用户发送好友申请")
    public Result<String> sendFriendRequest(@Validated @RequestBody ChatFriendRequestDTO requestDTO) {
        String userId = UserContext.getUserId();
        String requestId = friendService.sendFriendRequest(requestDTO, userId);
        return Result.success(requestId);
    }

    @PostMapping("/request/{requestId}/handle")
    @Operation(summary = "处理好友申请", description = "同意或拒绝好友申请")
    public Result<Boolean> handleFriendRequest(
            @Parameter(description = "申请ID") @PathVariable String requestId,
            @Parameter(description = "是否接受") @RequestParam Boolean accept,
            @Parameter(description = "回复消息") @RequestParam(required = false) String responseMessage) {
        String userId = UserContext.getUserId();
        Boolean result = friendService.handleFriendRequest(requestId, accept, responseMessage, userId);
        return Result.success(result);
    }

    @DeleteMapping("/{friendId}")
    @Operation(summary = "删除好友", description = "删除指定好友")
    public Result<Boolean> deleteFriend(
            @Parameter(description = "好友ID") @PathVariable String friendId) {
        String userId = UserContext.getUserId();
        Boolean result = friendService.deleteFriend(friendId, userId);
        return Result.success(result);
    }

    @PostMapping("/{friendId}/block")
    @Operation(summary = "拉黑好友", description = "将好友加入黑名单")
    public Result<Boolean> blockFriend(
            @Parameter(description = "好友ID") @PathVariable String friendId) {
        String userId = UserContext.getUserId();
        Boolean result = friendService.blockFriend(friendId, userId);
        return Result.success(result);
    }

    @PostMapping("/{friendId}/unblock")
    @Operation(summary = "取消拉黑", description = "将好友从黑名单移除")
    public Result<Boolean> unblockFriend(
            @Parameter(description = "好友ID") @PathVariable String friendId) {
        String userId = UserContext.getUserId();
        Boolean result = friendService.unblockFriend(friendId, userId);
        return Result.success(result);
    }

    @PutMapping("/{friendId}/remark")
    @Operation(summary = "更新好友备注", description = "修改好友的备注名称")
    public Result<Boolean> updateFriendRemark(
            @Parameter(description = "好友ID") @PathVariable String friendId,
            @Parameter(description = "备注名称") @RequestParam String remarkName) {
        String userId = UserContext.getUserId();
        Boolean result = friendService.updateFriendRemark(friendId, remarkName, userId);
        return Result.success(result);
    }

    @PutMapping("/{friendId}/group")
    @Operation(summary = "更新好友分组", description = "修改好友的分组")
    public Result<Boolean> updateFriendGroup(
            @Parameter(description = "好友ID") @PathVariable String friendId,
            @Parameter(description = "分组名称") @RequestParam String groupName) {
        String userId = UserContext.getUserId();
        Boolean result = friendService.updateFriendGroup(friendId, groupName, userId);
        return Result.success(result);
    }

    @PutMapping("/{friendId}/top")
    @Operation(summary = "设置好友置顶", description = "设置或取消好友置顶")
    public Result<Boolean> setFriendTop(
            @Parameter(description = "好友ID") @PathVariable String friendId,
            @Parameter(description = "是否置顶") @RequestParam Boolean isTop) {
        String userId = UserContext.getUserId();
        Boolean result = friendService.setFriendTop(friendId, isTop, userId);
        return Result.success(result);
    }

    @PutMapping("/{friendId}/disturb-free")
    @Operation(summary = "设置消息免打扰", description = "设置或取消消息免打扰")
    public Result<Boolean> setDisturbFree(
            @Parameter(description = "好友ID") @PathVariable String friendId,
            @Parameter(description = "是否免打扰") @RequestParam Boolean isDisturbFree) {
        String userId = UserContext.getUserId();
        Boolean result = friendService.setDisturbFree(friendId, isDisturbFree, userId);
        return Result.success(result);
    }

    @GetMapping("/search")
    @Operation(summary = "搜索用户", description = "根据关键词搜索用户")
    public Result<List<ChatFriendVO>> searchUsers(
            @Parameter(description = "搜索关键词") @RequestParam String keyword,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        String userId = UserContext.getUserId();
        List<ChatFriendVO> users = friendService.searchUsers(keyword, userId, limit);
        return Result.success(users);
    }

    @GetMapping("/groups")
    @Operation(summary = "获取好友分组", description = "获取用户的好友分组列表")
    public Result<List<String>> getFriendGroups() {
        String userId = UserContext.getUserId();
        List<String> groups = friendService.getFriendGroups(userId);
        return Result.success(groups);
    }

    @GetMapping("/requests")
    @Operation(summary = "获取好友申请", description = "获取好友申请列表")
    public Result<List<Object>> getFriendRequests(
            @Parameter(description = "类型") @RequestParam(defaultValue = "received") String type,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status) {
        String userId = UserContext.getUserId();
        List<Object> requests = friendService.getFriendRequests(userId, type, status);
        return Result.success(requests);
    }

    @GetMapping("/check/{friendId}")
    @Operation(summary = "检查好友关系", description = "检查是否为好友关系")
    public Result<Boolean> checkIsFriend(
            @Parameter(description = "好友ID") @PathVariable String friendId) {
        String userId = UserContext.getUserId();
        Boolean isFriend = friendService.checkIsFriend(userId, friendId);
        return Result.success(isFriend);
    }

    @GetMapping("/count")
    @Operation(summary = "统计好友数量", description = "获取好友数量统计")
    public Result<Integer> countFriends(
            @Parameter(description = "关系状态") @RequestParam(required = false) Integer status) {
        String userId = UserContext.getUserId();
        Integer count = friendService.countFriends(userId, status);
        return Result.success(count);
    }
}
